﻿namespace MyLibrary.API.Data
{
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Configuration;
    using System.Data;
    using System.Data.OleDb;
    using System.Data.SqlClient;
    using System.Data.Common;
    using System.Reflection;

    [Serializable()]
    internal class Database
    {
        private string connectionString;

        public Database()
        {
            connectionString = "";
        }

        public Database(string connectionString)
        {
            this.connectionString = connectionString;
        }

        public TRea ExecuteReader<TCon, TCom, TRea>(string query)
        {
            TCon connection = (TCon) Activator.CreateInstance(typeof(TCon), connectionString);
            TCom command = (TCom)Activator.CreateInstance(typeof(TCom), query, connection);            

            MethodInfo _Open = typeof(TCon).GetMethod("Open");
            _Open.Invoke(connection, null);

            MethodInfo _ExecuteReader = typeof(TCom).GetMethod("ExecuteReader", new Type[] { typeof(CommandBehavior) } );

            return (TRea) _ExecuteReader.Invoke(command, new object[] { CommandBehavior.CloseConnection });
        }        
    }
}

